<?php

use hhz\constant\redis\redisKeys;
use Biz\cache\album\AlbumRecommendCache;
use Biz\model\Album\AlbumListModel;
use Biz\service\Album\AlbumListService;
use Doraemon\cache\AlbumCounter;
use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\tools\BlockType;
use Doraemon\tools\Hhzurl;
use Hhz\Redis\newCache;

/**
 *
 */
class RecommAlbumAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    public function main()
    {
        $newRedis = new newCache(redisKeys::KEY_B_CACHE_ALBUM_RECOMMEND_OFFICIAL_LIST);
        $aData = $newRedis->getCache();
        $albumIds = $aData ? explode(',', $aData):[];

        $albumInfos = AlbumListService::getAlbumListByIds($albumIds, ['status' => AlbumListModel::STATS_LIST_PUBLIC_SHOW_MAP, 'ORDER' => ['album_id' => $albumIds]]);
        if ($albumInfos && is_array($albumInfos)) {
            $formatInfos = AlbumListService::formatAlbums($albumInfos);
            if ($formatInfos && is_array($formatInfos)) {
                $albumCounters = AlbumCounter::getMulti($albumIds);
                foreach ($formatInfos as $key => &$val) {
                    $statSign = basicStatSign::genStatSign(basicStatSign::FINE_GOODS_PAGE_ALBUM_LIST, [
                        basicStatSign::EXTEND_KEY_OBJ_TYPE => BlockType::TYPE_WIKI_ALBUM,
                        basicStatSign::EXTEND_KEY_OBJ_ID => $val['album_info']['album_id'],
                        basicStatSign::EXTEND_KEY_POS => 'order_first',
                        basicStatSign::EXTEND_KEY_INDEX => $key,
                    ], []);
                    $url = Hhzurl::getAlbumH5Link($val['album_info']['album_id']);
                    $val['album_info']['album_link'] = 'niceliving://webview/fullscreen?protego=' . urlencode(base64_encode(json_encode(['url' => $url, 'statSign' => urlencode(base64_encode($statSign))])));
                    $val['statSign'] = $statSign;
                    $val['album_info']['joiner_num'] = $albumCounters[$val['album_info']['album_id']]['read'] ?: 0;
                }
                $formatInfos = array_values($formatInfos);
            }
        }
        $more_url = Hhzurl::getDomain() . "/brand/good-thing-list";
        $more_album_url = 'niceliving://webview/fullscreen?protego=' . urlencode(base64_encode(json_encode(['url' => $more_url])));


        $this->setSuccess(['list' => $formatInfos, 'more_album_link' => $more_album_url]);

    }
}